/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vng.survey.controller;

import com.vng.zing.survey.thrift.client.SurveyClient;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import vng.survey.model.CommonModel;
import com.vng.zing.survey.thrift.model.Error;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

/**
 *
 * @author namhh
 */
public class AjaxDeleteResponse extends BaseController {

    @Override
    public String render(HttpServletRequest request, HttpServletResponse resp) throws IOException, Exception {
        int idSurvey = Integer.parseInt(request.getParameter("idSurvey"));
        String myJsonData = request.getParameter("arrID");
        String[] arrID = myJsonData.split(",");
        if(arrID.length<=0){
            return CommonModel.toJSON("Thất bại", "Không có gì để xóa!");
        }
        
         JSONObject jSONObject = new JSONObject();
        jSONObject.put("idSurvey", idSurvey);
        JSONArray array = new JSONArray();
        SurveyClient surveyClient = SurveyClient.getInstance();
        for (String string : arrID) {
            int id =  Integer.parseInt(string);
          if(surveyClient.removeResponse(idSurvey, id)== Error.SUCCESS){
              array.add(id);
          }
        }
       jSONObject.put("idUser", array);
       return jSONObject.toJSONString();
    }

   
}
